﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CodeCube.Common.CryptHelper
{
    public class CRC16Crypt
    {
        public static int CRC16Computer(byte[] InputData, int nlen, ref byte[] ResCRC16)
        {
            ushort Reg, temp, Crccode, i, j;
            Reg = 0xFFFF;
            Crccode = 0xA001;
            for (i = 0; i < nlen; i++)
            {
                Reg ^= InputData[i];
                for (j = 0; j < 8; j++)
                {
                    temp = (ushort)(Reg & 0x0001);
                    Reg = (ushort)(Reg >> 1);
                    if (temp == 0x0001)
                        Reg ^= Crccode;
                }
            }
            ResCRC16[1] = (byte)(Reg & 0xff);
            ResCRC16[0] = (byte)((Reg >> 8) & 0xff);
            return 1;
        }
    }
}
